//Source file: H:\\temp\\generated\\multidiagram\\NestedEditorDescriptor.java

package com.cea.papyrus.multidiagram.extension;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.Plugin;
import org.eclipse.gef.ui.parts.GraphicalEditor;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.plugin.AbstractUIPlugin;


/**
 * This descriptor describes a nested diagram. It is used by MultiDiagramEditor to 
 * know about the nested diagram. 
 * It is fill by an extension.
 */
public class NestedEditorDescriptor 
{
   /**
    * Descriptor of the button used to create a new editor.
    */
   private ActionBarButtonDescriptor createButtonDescriptor;
   /**
    * Editor factory implementation class.
    */
   private Class editorFactoryClass;
   
   /**
    * Cached factory. Initialized by first call to getXxx
    */
   private IEditorFactory editorFactory;
   
   /**
    * 
    */
   protected NestedDiagram editor;
   
   /**
    * Constructor.
    */
   public NestedEditorDescriptor() 
   {
    
   }
   
   /**
    * Get associated editorFactory.
    * The editorFactoryClass should be set, otherwise an error is thrown.
    * @return
    */
   public IEditorFactory getEditorFactory()
   {
		if(editorFactory!=null)
			return editorFactory;
		
		if(editorFactoryClass==null)
		{  // error
			throw new IllegalStateException("EditorFactory class should be set.");
		}
		
		// Create it
		try {
			editorFactory = (IEditorFactory) editorFactoryClass.newInstance();
			return editorFactory;
		} catch (InstantiationException e) {
			// Lets propagate. This is an implementation problem that should be solved by programmer.
			throw new RuntimeException(e);
		} catch (IllegalAccessException e) {
			// Lets propagate. This is an implementation problem that should be solved by programmer.
			throw new RuntimeException(e);
		}
   }
	  
   /**
    * REturn true if the editor can edit the root object.
    * @param root
    * @return boolean
     */
   public boolean isEditorFor(Object root) 
   {
    return getEditorFactory().isEditorFor(root);
   }
   
   /**
    * Create anew editor for the specified root object.
    * @param sharedObjects
    * @param root
    * @return multidiagram.NestedDiagram
 * @throws InvocationTargetException 
 * @throws IllegalAccessException 
 * @throws InstantiationException 
 * @throws IllegalArgumentException 
    */
   public IEditorPart createEditor(MultiDiagramSharedObjects sharedObjects, Object root)  
   {
	   return getEditorFactory().createEditorFor(sharedObjects, root);
   }


public Class getEditorFactoryClass() {
	return editorFactoryClass;
}

public void setEditorFactoryClass(Class editorFactoryClass) {
	this.editorFactoryClass = editorFactoryClass;
}

/**
 * @return the createButtonDescriptor
 */
public ActionBarButtonDescriptor getCreateButtonDescriptor() {
    return createButtonDescriptor;
}

/**
 * @param createButtonDescriptor the createButtonDescriptor to set
 */
public void setCreateButtonDescriptor(ActionBarButtonDescriptor createButtonDescriptor) {
    this.createButtonDescriptor = createButtonDescriptor;
}

public String toString()
{
    return super.toString() + " factory=" + editorFactory + "\n   create=" + createButtonDescriptor;
}
}
